from functools import wraps

from flask import g
from flask import session, jsonify
from werkzeug.routing import BaseConverter

from ihome.utils.response_code import RET


class RegexConverter(BaseConverter):
    """自定义正则转换器"""

    def __init__(self, url_map, *args):
        super(RegexConverter, self).__init__(url_map)
        self.regex = args[0]
        
        
def login_required(f):
    """定义一个装饰器,用来判断用户的登录状态"""

    @wraps(f)
    def wrapper(*args,**kwargs):
        # 获取用户的登录信息
        user_id = session.get("user_id")
        # 如果没有获取到用户,代表用户没有登录
        if user_id is None:
            return jsonify(errno=RET.SESSIONERR,errmsg="用户未登录")
        
        else:
            g.user_id = user_id
            return f(*args,**kwargs)
    return wrapper
            
            
            
            